package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @CacheEvict(cacheNames="setmealCache" , allEntries=true)
    public R save(@RequestBody SetmealDto setmealDto){
        log.info("套餐新增");

        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize ,String name){

            //分页构造器对象
            Page<Setmeal> pageInfo = new Page<>(page,pageSize);
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(name != null,Setmeal::getName,name);//添加查询条件，根据name进行like模糊查询
            queryWrapper.orderByDesc(Setmeal::getUpdateTime);//添加排序条件，根据更新时间降序排列
            setmealService.page(pageInfo,queryWrapper);

            Page<SetmealDto> dtoPage = new Page<>();
            //封装数据-total
            dtoPage.setTotal(pageInfo.getTotal());
            //封装数据-records
            List<Setmeal> records = pageInfo.getRecords();//原始的数据列表
            List<SetmealDto> list = records.stream().map((setmeal) -> {
                SetmealDto setmealDto = new SetmealDto();
                BeanUtils.copyProperties(setmeal,setmealDto);//对象拷贝

                //根据分类id查询分类对象
                Category category = categoryService.getById(setmeal.getCategoryId());
                if(category != null){
                    setmealDto.setCategoryName(category.getName()); //分类名称
                }
                return setmealDto;
            }).collect(Collectors.toList());
            dtoPage.setRecords(list);
            return R.success(dtoPage);
        }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames="setmealCache",allEntries=true)
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        return R.success();
    }

    @GetMapping("/{id}")
    public R<SetmealDto> getSetmeal(@PathVariable Long id){

        SetmealDto hUixian = setmealService.getHUixian(id);

        return R.success(hUixian);
    }

    @PutMapping
    public R putSetmeal(@RequestBody SetmealDto setmealDto){

        setmealService.save1(setmealDto);
        return R.success();
    }

    @PostMapping("/status/{status}")
    public R<Setmeal> pustsetmeal2(@PathVariable int status, @RequestParam  List<Long> ids){

        if (status == 0){
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Setmeal::getId,ids);
            List<Setmeal> list = setmealService.list(queryWrapper);
            for (Setmeal setmeal : list) {
                setmeal.setStatus(0);
            }
            setmealService.updateBatchById(list);


        }else {
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Setmeal::getId,ids);
            List<Setmeal> list = setmealService.list(queryWrapper);
            for (Setmeal setmeal : list) {
                setmeal.setStatus(1);
            }
            setmealService.updateBatchById(list);
        }
        return R.success();


    }

    @GetMapping("/dish/{id}")
    public R<List<SetmealDish>> dish(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        return R.success(list);

    }

    @GetMapping("list")
    @Cacheable(cacheNames="setmealCache" , key= "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> setmeal(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null , Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null , Setmeal::getStatus,setmeal.getStatus());

        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list=setmealService.list(queryWrapper);
        return R.success(list);
    }
}
